package com.example.renyang.happysell;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.renyang.happysell.MyUntil.Data;
import com.example.renyang.happysell.net.HttpUtil;

public class RegisterActivity extends AppCompatActivity {
    TextView uname=null;
    TextView password=null;
    TextView phonenum=null;
    String unamet=null;
    String passwordt=null;
    String phonenumt=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        uname=(TextView)findViewById(R.id.regusernameET);
        password=(TextView)findViewById(R.id.regpasswordET);
        phonenum=(TextView)findViewById(R.id.regphonenum);
        TextView tvlogin=(TextView)findViewById(R.id.directlogTV);
        Button loginbt=(Button)findViewById(R.id.registerBtn);
        tvlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        loginbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unamet=uname.getText().toString();
                passwordt=password.getText().toString();
                phonenumt=phonenum.getText().toString();
                try {
              //      final JSONArray[] obj={new JSONArray()};

                    new Thread(){
                       public void run(){
                           try {
                               String ss = query0(unamet, passwordt, phonenumt);
  //                             Log.i("zx",ss+""+">>>>>>>>>>>>>>>>>>>>>>>");

                 //              Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                               Data.setName(unamet);
                               Data.setTele(phonenumt);
                               Intent intent=new Intent();
                               intent.setClass(RegisterActivity.this,LoginActivity.class);
                               startActivity(intent);

                           }catch (Exception e){
                               Log.i("zx","注册失败");
                               e.printStackTrace();
                           }
                       }
                    }.start();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });


    }

    private String query0(String unamet, String passwordt, String phonenumt1) throws Exception {

  //      String url=HttpUtil.BASE_URL+"UserRegist.html";//?uname="+unamet+"&upassword="+passwordt+"&tele="+phonenumt1;
        String url=HttpUtil.BASE_URL+"userRegist?uname="+unamet+"&upassword="+passwordt+"&tele="+phonenumt1;
    /*    Map map = new HashMap();
        map.put("uname",unamet);
        map.put("upassword",passwordt);
        map.put("tele",phonenumt1);
        String s=HttpUtil.postRequest(url,map);
        */
        String s= HttpUtil.getRequest(url);
                return s;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_register, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
